package com.android.view.activity.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.presenter.info.SetTradePWDPresenter;
import com.android.presenter.iview.ISetTradePWDView;
import com.android.util.ToastUtil;
import com.android.view.activity.base.Constants;
import com.android.view.activity.base.ToolbarBaseActivity;
import com.android.R;

import butterknife.BindView;
import retrofit2.Response;

/**
 * 重设密码，输入两次
 * Created by TenFace on 2018/4/23.
 */

public class SetNewTradePasswordActivity extends ToolbarBaseActivity implements ISetTradePWDView {
    private static final String TAG = RegisterActivity.class.getSimpleName();
    @BindView(R.id.etNewPWD)
    EditText etNewPWD;
    @BindView(R.id.etNewPWDAgain)
    EditText etNewPWDAgain;
    @BindView(R.id.btnSubmitNewPWD)
    Button btnSubmitNewPWD;
    private Context context;
    private static String intentKey = "TOKEN";
    private String token;
    private SetTradePWDPresenter setTradePWDPresenter;

    public static void actionStart(Context context, String token) {
        Intent intent = new Intent(context, SetNewTradePasswordActivity.class);
        intent.putExtra(intentKey, token);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Constants.activityList.put(TAG, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Constants.activityList.remove(TAG);
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_set_trade_password);
    }

    @Override
    protected void initView() {
        context = this;
        int color = getResourceColor(R.color.white);
        setBarColor(color, 0, color, 0);
        setStatusBarMode(true);
        Intent intent = getIntent();
        token = intent.getStringExtra(intentKey);
    }

    @Override
    protected void obtainData() {
        setTradePWDPresenter = new SetTradePWDPresenter(this);
    }

    @Override
    protected void initEvent() {
        btnSubmitNewPWD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etNewPWD.getText().length() < 6) {
                    ToastUtil.show("请输入6位以上密码！");
                } else if (etNewPWDAgain.getText().length() < 6) {
                    ToastUtil.show("请输入6位以上密码！");
                } else if (!etNewPWD.getText().toString().equals(etNewPWDAgain.getText().toString())) {
                    ToastUtil.show("两次密码输入不一致，请重试！");
                } else {
                    setTradePWDPresenter.setTradePassword(token, etNewPWD.getText().toString(), "SET_TRADE_PASSWORD");
                }
            }
        });
    }

    @Override
    public void setTradePWDSuccess(Response<Void> voidResponse) {

        if (voidResponse.code() == 200) {
            ToastUtil.show("修改密码成功！");
            Constants.activityList.get("TradePWDVerifySecurityActivity").finish();
            finish();
        }

    }

    @Override
    public void setTradePWDFail(int status, String desc) {
        ToastUtil.show("请求失败，请稍后重试！");
    }
}
